<?php
/**
 * User: lsx
 * Date: 2017/6/2
 * Time: 10:30
 * Progress:0%
 */

namespace app\shop\controller;


use icontroller\admin\AController;
use icontroller\admin\shop\IShopGoods;
use logic\admin\shop\LShopGoods;
use model\ShopGoodsCategory;
use think\Db;
use model\ShopStore;
use model\ShopGoodsVariant;

class ShopGoods extends AController implements IShopGoods
{
    private $logic;

    public function __construct()
    {
        parent::__construct();
        $this->logic = new LShopGoods();
    }

    public function index()
    {
        $param = array(
            'store_id'=> input('store_id')
        );
        $cal = $this->logic->logic_index($param);
        $this->assign('cal', $cal);

        //获取下拉框店铺信息
        if(!empty($param['store_id'])){
            $store = $this->get_store($param['store_id']);
        }else{
            $store = $this->get_full_store();
        }

        $this->assign('store_info', $store);

        return view();
    }

    public function index_data()
    {
        $param = array(
            'search' => input('search'),
            'limit' => input('limit'),
            'offset' => input('offset'),
            'sort' => input('sort'),
            'order' => input('order'),
            'flag_putaway' => input('flag_putaway'),
            'type' => input('type'),
            'store_id'=> input('store_id')
        );
        return $this->logic->logic_index_data($param);
    }


    public function add()
    {
        $store_id = $this->request->param('store_id');
        //从店铺列表进入商品管理
        if($store_id > 0){
            //查询指定店铺
            $store = $this->get_store($store_id);
            //查询指定店铺下的所有分类
            $category = $this->get_category($store_id);
        }else{
            $store = $this->get_full_store();
            //默认查询第一家店铺下的分类
            $category = $this->get_category($store['rows'][0]['id']);
        }

        $this->assign('store', $store);
        $this->assign('category', $category);

//        $variant = $this->get_variant();
//        $this->assign('variant', $variant);
        return view();
    }

    public function insert()
    {
        $params1 = array(
            'name' => input('name'),
            'category_id' => 0,
            'store_id' => input('store_id'),
            'category_tree' => input('category_tree'),
            'intro' => input('intro'),
            'flag_putaway' => input('flag_putaway'),
            'price' => input('price'),
            'detail' => input('detail'),
            'sort' => input('sort'),
            'price_original' => input('price_original'),
            'cover_image' => input('cover_image_id'),
            'slide_image' => input('slide_image_id'),
            'sales' => input('sales'),
            'is_rec' => input('is_rec')
        );
        $params2 = array(
            'variant_name' => input('variant_name/a'),
            'variant_price' => input('variant_price/a')
        );

        return $this->logic->logic_insert($params1,$params2);
    }

    public function edit()
    {
        $id = input('id');
        $data = $this->logic->logic_edit($id);
        $this->assign('data',$data);

        $store_id = $this->request->param('store_id');
        if($store_id > 0){
            $store = $this->get_store($store_id);
            $category = $this->get_category($store_id);
        }else{
            $store = $this->get_full_store();
            $category = $this->get_category($data['store_id']);
        }

        $this->assign('store', $store);
        $this->assign('category', $category);

        $variant = $this->get_variant($id);
        $this->assign('variant', $variant);

        return view();
    }

    public function update()
    {

        $params = array(
            'id' => input('id'),
            'name' => input('name'),
            'category_id' => 0,
            'store_id' => input('store_id'),
           // 'variant_id' => input('variant_id/a'),
            'category_tree' => input('category_tree'),
            'intro' => input('intro'),
            'flag_putaway' => input('flag_putaway'),
            'price' => input('price'),
            'detail' => input('detail'),
            'sort' => input('sort'),
            'price_original' => input('price_original'),
            'cover_image' => input('cover_image_id'),
            'slide_image' => input('slide_image_id'),
            'sales' => input('sales'),
            'is_rec' => input('is_rec')
        );

        $params2 = array(
            'variant_id' => input('variant_id/a'),
            'variant_name' => input('variant_name/a'),
            'variant_price' => input('variant_price/a'),
            'deleted_variant_ids' => input('deleted_variant_ids')
        );

        return $this->logic->logic_update($params,$params2);

    }


    public function up()
    {
        return $this->updateField('flag_putaway', 1, "上架成功");
    }

    public function down()
    {
        return $this->updateField('flag_putaway', 0, "下架成功");
    }

    public function delete()
    {
        $id = input('id');
        return $this->logic->logic_delete($id);
    }

    public function uploadImg()
    {

    }

    public function get_category($store_id)
    {
        $where['flag_delete'] = 0;
        $where['flag_use'] = 1;
        if(!empty($store_id)){
            $where['store_id'] = $store_id;
        }
        $total = Db::name('shop_goods_category')->where($where)->count();
        $data = Db::name('shop_goods_category')->where($where)->field('id,name')->select();

        return array(
            'total' => $total,
            'rows' => $data
        );
    }

    // <editor-fold defaultstate="collapsed" desc="获取商家">
    public function get_store($id)
    {
        $store = new ShopStore();
        $data = $store->where('id',$id)->field('id,name as text')->select();
        return array(
            'total' => count($data),
            'rows' => $data
        );
    }
    public function get_full_store()
    {
        $store = new ShopStore();
        $total = $store->count();
        $data = $store->field('id,name as text')->select();
        return array(
            'total' => $total,
            'rows' => $data
        );
    }
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="获取规格">
    public function get_variant($goods_id)
    {
        $where = [
            'goods_id' => $goods_id,
            'flag_delete'=>0
        ];
        $variant = new ShopGoodsVariant();
        $total = $variant->where($where)->count();
        $data = $variant->where($where)->field('id,name,price,goods_id')->select();

        return array(
            'total' => $total,
            'rows' => $data
        );
    }

    // </editor-fold>
    function get_top_parentid($id, $str = null, $ids = array())
    {
        $model = new ShopGoodsCategory();
        $r = $model->where(['id' => $id])->find();
        if ($r) {
            $ids[] = $r->getData()['id'];
            if ($str == null) {
                $str = $r->getData()['name'];
            } else {
                $str = $r->getData()['name'] . "-" . $str;
            }

            if ($r->getData()['parent_id'] != '0') {
                return $this->get_top_parentid($r['parent_id'], $str, $ids);
            }
        }
        return $str;
    }
}